#ifndef PD_IOS_H
#define PD_IOS_H

#define ENABLE_UDK_DEBUG 1
#define ENABLE_UDK_PRINT_PREFIX 1

//#define _XOPEN_SOURCE
#include <platform.h>
#include <sys/time.h>
#include <sys/select.h> 
#include <unistd.h>

#include <dlfcn.h>

#include <platform.h>

#define PD_UDK_API 
#define PD_DLLIMPORT
#define PD_RTLIBPREFIX "lib"
#define PD_RTLIBSUFFIX ".so"

#ifndef CONDITION
#define CONDITION(cond)     (__builtin_expect((cond)!=0, 0))
#endif

/*
 * Log a fatal error.  If the given condition fails, this stops program
 * execution like a normal assertion, but also generating the given message.
 * It is NOT stripped from release builds.  Note that the condition test
 * is -inverted- from the normal assert() semantics.
 */
#define LOG_ALWAYS_FATAL_IF(cond,format,args...) \
    ( (CONDITION(cond)) \
    ? ((void)fprintf(stderr,format, ##args)) \
    : (void)0 )

#define LOG_ALWAYS_FATAL(format,args...) \
    ( ((void)fprintf(stderr,format, ##args)) )

#if (ENABLE_UDK_PRINT_PREFIX==1)
#define UDK_PRINT_PREFIX(out,prio) \
	do {	\
	char timeBuf[32];		\
	char prefixBuf[128]={0};	\
	struct timeval tv;	\
	struct timezone tz;	\
	size_t prefixLen;	\
	gettimeofday(&tv, &tz);	\
	struct tm* ptm = localtime(&tv.tv_sec);	\
	strftime(timeBuf, sizeof(timeBuf), "%m-%d %H:%M:%S", ptm);	\
	prefixLen = snprintf(prefixBuf, sizeof(prefixBuf),	\
                "%s.%06ld %c: ", timeBuf, tv.tv_usec,	\
                prio);	\
	if(prefixLen >= sizeof(prefixBuf))	\
		prefixLen = sizeof(prefixBuf) - 1;	\
		fprintf(out,"%s",prefixBuf);	\
	}while(0)
#else
#define UDK_PRINT_PREFIX(out,prio)
#endif

#define UDK_PRINT(format, args...) do { UDK_PRINT_PREFIX(stderr, 'D'); fprintf(stderr,format, ##args); } while (0)
#if (ENABLE_UDK_DEBUG==1)
#define UDK_LOGD(format, args...) do { UDK_PRINT_PREFIX(stderr,'D');fprintf(stderr,format, ##args); }while (0)
#else
#define UDK_LOGD(format, args...)
#endif
#define UDK_LOGI(format, args...) do { UDK_PRINT_PREFIX(stderr,'I');fprintf(stderr,format, ##args); }while (0)
#define UDK_LOGW(format, args...) do { UDK_PRINT_PREFIX(stderr,'W');fprintf(stderr,format, ##args); }while (0)
#define UDK_LOGE(format, args...) do { UDK_PRINT_PREFIX(stderr,'E');fprintf(stderr,format, ##args); }while (0)
#define UDK_LOGFATAL(...) LOG_ALWAYS_FATAL(__VA_ARGS__)
#define UDK_LOGIF(cond,format,args...) \
	( (CONDITION(cond)) \
    ? (fprintf(stderr,format, ##args)) \
    : (void)0 )
#define UDK_LOGFATAL_IF(cond,...) LOG_ALWAYS_FATAL_IF(cond, __VA_ARGS__)

#define UDK_DUMP(prefix,d,l) udk_dump(prefix,d,l)

#endif

